﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using timetableasy.Models;

namespace timetableasy.Controllers
{
    public class ClassRoomsController : Controller
    {
        ClassRoomRepository classRoomRepository = new ClassRoomRepository();
        //
        // GET: /ClassRooms/

        public ActionResult Index()
        {
            var classRooms = classRoomRepository.FindAllClassRooms().ToList();
            return View("Index", classRooms);
        }

        //
        // GET: /ClassRooms/Details/5

        public ActionResult Details(int id)
        {
            ClassRoom classRoom = classRoomRepository.GetClassRoomById(id);
            if (classRoom == null)
                return View("NotFound");
            else
                return View("Details", classRoom);
        }

        //
        // GET: /ClassRooms/Create

        public ActionResult Create()
        {
            ClassRoom classRoom = new ClassRoom()
            {
                ClassRoomName = string.Empty,
                Campus= new Campus()
            };

            return View(classRoom);
        }

        //
        // POST: /ClassRooms/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(ClassRoom classRoom)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    classRoomRepository.Add(classRoom);
                    classRoomRepository.Save();

                    return RedirectToAction("Details", new { id = classRoom.ClassRoomId });
                }
                catch
                {
                    foreach (var issue in classRoom.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }
                }
            }

            return View(classRoom);
        }

        //
        // GET: /ClassRooms/Edit/5

        public ActionResult Edit(int id)
        {
            ClassRoom classRoom = classRoomRepository.GetClassRoomById(id);
            if (classRoom != null)
            {
                return View(classRoom);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // POST: /ClassRooms/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            ClassRoom classRoom = classRoomRepository.GetClassRoomById(id);
            if (classRoom == null)
                return View("NotFound");
            try
            {
                UpdateModel(classRoom);
                classRoomRepository.Save();
                return RedirectToAction("Details", new { id = classRoom.ClassRoomId });
            }
            catch
            {
                foreach (var issue in classRoom.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(classRoom);
            }
        }

        //
        // GET: /ClassRooms/Delete/5

        public ActionResult Delete(int id)
        {
            ClassRoom classRoom = classRoomRepository.GetClassRoomById(id);

            if (classRoom == null)
                return View("NotFound");

            //if (!ClassRoom.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            return View(classRoom);
        }

        // 
        // HTTP POST: /ClassRooms/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            ClassRoom classRoom = classRoomRepository.GetClassRoomById(id);

            if (classRoom == null)
                return View("NotFound");

            //if (!ClassRoom.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            classRoomRepository.Delete(classRoom);
            classRoomRepository.Save();

            return View("Deleted");
        }
    }
}
